package socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author MaoLin Wang
 * @description
 * @date 2020/9/30 8:56 上午
 */
public class Server {
    private static  ServerSocket serverSocket;
    private static ThreadPoolExecutor executor=new ThreadPoolExecutor(2,4,1, TimeUnit.MINUTES,
            new ArrayBlockingQueue<>(1),new ThreadPoolExecutor.CallerRunsPolicy());

    static {
        try {
            serverSocket = new ServerSocket();
            serverSocket.bind(new InetSocketAddress("127.0.0.1",9001));
            Socket socket;
            while ((socket=serverSocket.accept())!=null){
                executor.execute(new ServerRunnable(socket));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

    }
}
